package com.whoimi.admin.service.impl;

import com.whoimi.admin.entity.vo.VisitStatsVO;
import com.whoimi.admin.entity.vo.VisitTrendVO;
import com.whoimi.admin.repository.LogRepository;
import com.whoimi.admin.service.LogService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author whoimi
 * @since 2025-07-28
 **/
@Service
public class LogServiceImpl implements LogService {
    @Resource
    private LogRepository logRepository;

    @Override
    public VisitTrendVO getVisitTrend(LocalDate startDate, LocalDate endDate) {
        VisitTrendVO visitTrend = new VisitTrendVO();
        List<String> dates = new ArrayList<>();

        // 获取日期范围内的日期
        while (!startDate.isAfter(endDate)) {
            dates.add(startDate.toString());
            startDate = startDate.plusDays(1);
        }
        visitTrend.setDates(dates);

        // 获取访问量和访问 IP 数的统计数据
        List<Map<String, Object>> pvCounts = logRepository.getPvCounts(dates.get(0) + " 00:00:00", dates.get(dates.size() - 1) + " 23:59:59");
        List<Map<String, Object>> ipCounts = logRepository.getIpCounts(dates.get(0) + " 00:00:00", dates.get(dates.size() - 1) + " 23:59:59");

        Map<String, Object> pvMap = Map.of();
        if (!pvCounts.isEmpty()) {
            pvMap = pvCounts.get(0);
        }
        Map<String, Object> ipMap = Map.of();
        if (!pvMap.isEmpty()) {
            ipMap = ipCounts.get(0);
        }
        // 匹配日期和访问量/访问 IP 数
        List<Integer> pvList = new ArrayList<>();
        List<Integer> ipList = new ArrayList<>();

        for (String date : dates) {
            pvList.add((Integer) pvMap.getOrDefault(date, 0));
            ipList.add((Integer) ipMap.getOrDefault(date, 0));
        }

        visitTrend.setPvList(pvList);
        visitTrend.setIpList(ipList);

        return visitTrend;
    }

    @Override
    public VisitStatsVO getVisitStats() {

        VisitStatsVO result = new VisitStatsVO();
        // 访客数统计(UV)
        List<Map<String, Object>> uvStats = logRepository.getUvStats();
        if (!uvStats.isEmpty()) {
            Map<String, Object> map = uvStats.get(0);
//            result.setTodayUvCount((Integer) map.get("todayCount"));
//            result.setTotalUvCount((Integer) map.get("totalCount"));
//            result.setUvGrowthRate(new BigDecimal(map.get("growthRate").toString()));

            Object todayCountObj = map.get("todayCount");
            Object totalCountObj = map.get("totalCount");
            Object growthRateObj = map.get("growthRate");

            result.setTodayPvCount(todayCountObj != null ?
                    Integer.parseInt(todayCountObj.toString()) : 0);

            result.setTotalPvCount(totalCountObj != null ?
                    Integer.parseInt(totalCountObj.toString()) : 0);

            result.setPvGrowthRate(growthRateObj != null ?
                    new BigDecimal(growthRateObj.toString()) : BigDecimal.ZERO);
        }


        // 浏览量统计(PV)
        List<Map<String, Object>> pvStats = logRepository.getPvStats();
        if (!pvStats.isEmpty()) {
            Map<String, Object> map = pvStats.get(0);
//            result.setTodayPvCount((Integer) map.get("todayCount"));
//            result.setTotalPvCount((Integer) map.get("totalCount"));
//            result.setPvGrowthRate(new BigDecimal(map.get("growthRate").toString()));

            result.setTodayUvCount(map.get("todayCount") != null ?
                    Integer.parseInt(map.get("todayCount").toString()) : 0);
            result.setTotalUvCount(map.get("totalCount") != null ?
                    Integer.parseInt(map.get("totalCount").toString()) : 0);
            result.setUvGrowthRate(map.get("growthRate") != null ?
                    new BigDecimal(map.get("growthRate").toString()) : BigDecimal.ZERO);
        }
        return result;
    }
}
